package so.sao.code.service.service.impl;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.tools.SimpleJavaFileObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import so.sao.code.service.domain.entity.EnterpriseCustomField;
import so.sao.code.service.infrastructure.annotation.Permission;
import so.sao.code.service.infrastructure.enums.CodeMessage;
import so.sao.code.service.infrastructure.enums.MODE_SELECTION;
import so.sao.code.service.infrastructure.enums.PrintFileFieldConfig;
import so.sao.code.service.infrastructure.enums.ROLE;
import so.sao.code.service.infrastructure.enums.TAG_BATCH_TYPE;
import so.sao.code.service.infrastructure.model.vo.EnterpriseCustomFieldVO;
import so.sao.code.service.infrastructure.utils.CustomDefinitionParseUtils;
import so.sao.code.service.repository.EnterpriseCustomFieldDao;
import so.sao.code.service.service.EnterpriseCustomFieldService;
import so.sao.tag.core.TagCode;
import so.sao.top.core.util.dynamic.CustomCode;
import so.sao.top.core.util.dynamic.DynamicInvokeCustomCodeUtils;
import so.sao.top.core.util.rest.RestException;

@Service
public class EnterpriseCustomFieldServiceImpl implements EnterpriseCustomFieldService {

	@Autowired
	EnterpriseCustomFieldDao enterpriseCustomFieldDao;

	@Permission(role = ROLE.ADMIN)
	@Override
	public List<EnterpriseCustomField> findFieldsInfoS() {
		return enterpriseCustomFieldDao.findDistinctAll();
	}

	@Permission(role = ROLE.ADMIN)
	@Transactional
	@Override
	public List<EnterpriseCustomField> create(Integer mode,String eseId, String fieldName,String fieldKey, String fieldValue) {
		enterpriseCustomFieldDao.deleteByfieldKey(fieldKey);
		List<EnterpriseCustomField> enterpriseCustomField = enterpriseCustomFieldDao
				.findByfieldName(fieldName);
		if (enterpriseCustomField.size() != 0) {
			throw new RestException(HttpStatus.BAD_REQUEST, CodeMessage.CustomNameRepetition,
					fieldName);
		}
//		String fieldKey;
		if (MODE_SELECTION.CLASSIS_MODE.getValue().equals(mode)) {
			List<Object> list = new ArrayList<Object>();
		    list.add("http://www.baidu.com");
		    list.add("www.baidu.com");
		    list.add("Q");
		    list.add("XXXXYYYY");
		    list.add(1001);
		    list.add("mixed16");
		    String codeKey = "cccc";
		    System.out.println(fieldValue+"-------------------------------设置-----------------------------------------");
			  String result = (String) DynamicInvokeCustomCodeUtils.invokeCustomMethod(CustomCode.DefaultCodeTemplate, list,
					  fieldValue, codeKey);
			if (result==null) {
				throw new RestException(HttpStatus.BAD_REQUEST, CodeMessage.CodeCompilationFailed,
						fieldValue);
			}
			fieldKey = "Classic_" + UUID.randomUUID();
		} else {
			fieldKey = "Common_" + UUID.randomUUID();
		}

		List<EnterpriseCustomField> saveList = new ArrayList<>();
		String[] eIds = eseId.split(",");
		for (int i = 0; i < eIds.length; i++) {
			if (eIds[i] != null) {
				EnterpriseCustomField enterpriseAddfield = new EnterpriseCustomField();
				enterpriseAddfield.setEseId(Long.valueOf(eIds[i]));
				enterpriseAddfield.setDeleted(false);
				enterpriseAddfield.setFieldKey(fieldKey);
				enterpriseAddfield.setFieldName(fieldName);
				enterpriseAddfield.setFieldValue(fieldValue);
//				enterpriseAddfield.setFieldValue(customFieldCreate.getFieldValue());
				saveList.add(enterpriseAddfield);
			}
		}
		return enterpriseCustomFieldDao.save(saveList);
	}

	@Override
	public List<List<EnterpriseCustomField>> findFieldsInfoByEseIdAndCodeType(Long eseId, Integer codeType) {
		List<EnterpriseCustomField> result = new ArrayList<>();
		List<EnterpriseCustomField> enterpriseCustomField = enterpriseCustomFieldDao.findByEseId(eseId);
		List<List<EnterpriseCustomField>> resultList = new ArrayList<>();
		switch (TAG_BATCH_TYPE.fromValue(codeType)) {
		case NORMAL:
			result.add(new EnterpriseCustomField(eseId, PrintFileFieldConfig.CODEURL.getName(),
					PrintFileFieldConfig.CODEURL.getValue()));
			result.add(new EnterpriseCustomField(eseId, PrintFileFieldConfig.FRIENDLYCODE.getName(),
					PrintFileFieldConfig.FRIENDLYCODE.getValue()));
			result.add(new EnterpriseCustomField(eseId, PrintFileFieldConfig.FRIENDLYCODEFRONT.getName(),
					PrintFileFieldConfig.FRIENDLYCODEFRONT.getValue()));
			result.add(new EnterpriseCustomField(eseId, PrintFileFieldConfig.FRIENDLYCODEEND.getName(),
					PrintFileFieldConfig.FRIENDLYCODEEND.getValue()));
			result.add(new EnterpriseCustomField(eseId, PrintFileFieldConfig.SEQNUM.getName(),
					PrintFileFieldConfig.SEQNUM.getValue()));
			result.add(new EnterpriseCustomField(eseId, PrintFileFieldConfig.SEQNUMFRONT.getName(),
					PrintFileFieldConfig.SEQNUMFRONT.getValue()));
			result.add(new EnterpriseCustomField(eseId, PrintFileFieldConfig.SEQNUMEND.getName(),
					PrintFileFieldConfig.SEQNUMEND.getValue()));
			result.addAll(enterpriseCustomField);
			/*
			 * result.add(new EnterpriseCustomField(eseId,
			 * PrintFileFieldConfig.PACKAGECODEURL.getName(),
			 * PrintFileFieldConfig.PACKAGECODEURL.getValue())); result.add(new
			 * EnterpriseCustomField(eseId, PrintFileFieldConfig.BOXCODEURL.getName(),
			 * PrintFileFieldConfig.BOXCODEURL.getValue())); result.add(new
			 * EnterpriseCustomField(eseId, PrintFileFieldConfig.BOXCODEURLSEQ.getName(),
			 * PrintFileFieldConfig.BOXCODEURLSEQ.getValue()));
			 */
			resultList.add(result);
			return resultList;
		case ROLL:
			result.add(new EnterpriseCustomField(eseId, PrintFileFieldConfig.CODEURL.getName(),
					PrintFileFieldConfig.CODEURL.getValue()));
			result.add(new EnterpriseCustomField(eseId, PrintFileFieldConfig.FRIENDLYCODE.getName(),
					PrintFileFieldConfig.FRIENDLYCODE.getValue()));
			result.add(new EnterpriseCustomField(eseId, PrintFileFieldConfig.FRIENDLYCODEFRONT.getName(),
					PrintFileFieldConfig.FRIENDLYCODEFRONT.getValue()));
			result.add(new EnterpriseCustomField(eseId, PrintFileFieldConfig.FRIENDLYCODEEND.getName(),
					PrintFileFieldConfig.FRIENDLYCODEEND.getValue()));
			result.add(new EnterpriseCustomField(eseId, PrintFileFieldConfig.SEQNUM.getName(),
					PrintFileFieldConfig.SEQNUM.getValue()));
			result.add(new EnterpriseCustomField(eseId, PrintFileFieldConfig.SEQNUMFRONT.getName(),
					PrintFileFieldConfig.SEQNUMFRONT.getValue()));
			result.add(new EnterpriseCustomField(eseId, PrintFileFieldConfig.SEQNUMEND.getName(),
					PrintFileFieldConfig.SEQNUMEND.getValue()));
			result.addAll(enterpriseCustomField);
			resultList.add(result);
			return resultList;
		case PACKAGE:
			result.add(new EnterpriseCustomField(eseId, PrintFileFieldConfig.FRIENDLYCODE.getName(),
					PrintFileFieldConfig.FRIENDLYCODE.getValue()));
			result.add(new EnterpriseCustomField(eseId, PrintFileFieldConfig.FRIENDLYCODEFRONT.getName(),
					PrintFileFieldConfig.FRIENDLYCODEFRONT.getValue()));
			result.add(new EnterpriseCustomField(eseId, PrintFileFieldConfig.FRIENDLYCODEEND.getName(),
					PrintFileFieldConfig.FRIENDLYCODEEND.getValue()));
			result.add(new EnterpriseCustomField(eseId, PrintFileFieldConfig.SEQNUM.getName(),
					PrintFileFieldConfig.SEQNUM.getValue()));
			result.add(new EnterpriseCustomField(eseId, PrintFileFieldConfig.SEQNUMFRONT.getName(),
					PrintFileFieldConfig.SEQNUMFRONT.getValue()));
			result.add(new EnterpriseCustomField(eseId, PrintFileFieldConfig.SEQNUMEND.getName(),
					PrintFileFieldConfig.SEQNUMEND.getValue()));
			List<EnterpriseCustomField> packgeOut = new ArrayList<>();
			packgeOut.addAll(result);

			result.add(new EnterpriseCustomField(eseId, PrintFileFieldConfig.CODEURL.getName(),
					PrintFileFieldConfig.CODEURL.getValue()));
			packgeOut.add(new EnterpriseCustomField(eseId, PrintFileFieldConfig.PACKAGECODEURL.getName(),
					PrintFileFieldConfig.PACKAGECODEURL.getValue()));
			// packgeOut.add(new EnterpriseCustomField(eseId,
			// PrintFileFieldConfig.BOXCODEURL.getName(),
			// PrintFileFieldConfig.BOXCODEURL.getValue()));
			packgeOut.addAll(enterpriseCustomField);
			result.addAll(enterpriseCustomField);
			resultList.add(packgeOut);
			resultList.add(result);
			return resultList;
		case DUPLICATE:
			List<EnterpriseCustomField> duplicateOut = new ArrayList<>();
			result.add(new EnterpriseCustomField(eseId, PrintFileFieldConfig.CODEURL.getName(),
					PrintFileFieldConfig.CODEURL.getValue()));
			result.add(new EnterpriseCustomField(eseId, PrintFileFieldConfig.FRIENDLYCODE.getName(),
					PrintFileFieldConfig.FRIENDLYCODE.getValue()));
			result.add(new EnterpriseCustomField(eseId, PrintFileFieldConfig.FRIENDLYCODEFRONT.getName(),
					PrintFileFieldConfig.FRIENDLYCODEFRONT.getValue()));
			result.add(new EnterpriseCustomField(eseId, PrintFileFieldConfig.FRIENDLYCODEEND.getName(),
					PrintFileFieldConfig.FRIENDLYCODEEND.getValue()));
			result.add(new EnterpriseCustomField(eseId, PrintFileFieldConfig.SEQNUM.getName(),
					PrintFileFieldConfig.SEQNUM.getValue()));
			result.add(new EnterpriseCustomField(eseId, PrintFileFieldConfig.SEQNUMFRONT.getName(),
					PrintFileFieldConfig.SEQNUMFRONT.getValue()));
			result.add(new EnterpriseCustomField(eseId, PrintFileFieldConfig.SEQNUMEND.getName(),
					PrintFileFieldConfig.SEQNUMEND.getValue()));
			result.addAll(enterpriseCustomField);
			duplicateOut.addAll(result);
			resultList.add(duplicateOut);
			resultList.add(result);
			return resultList;
		case WRAP:
			result.add(new EnterpriseCustomField(eseId, PrintFileFieldConfig.PACKAGECODEURL.getName(),
					PrintFileFieldConfig.PACKAGECODEURL.getValue()));
			result.add(new EnterpriseCustomField(eseId, PrintFileFieldConfig.FRIENDLYCODE.getName(),
					PrintFileFieldConfig.FRIENDLYCODE.getValue()));
			result.add(new EnterpriseCustomField(eseId, PrintFileFieldConfig.FRIENDLYCODEFRONT.getName(),
					PrintFileFieldConfig.FRIENDLYCODEFRONT.getValue()));
			result.add(new EnterpriseCustomField(eseId, PrintFileFieldConfig.FRIENDLYCODEEND.getName(),
					PrintFileFieldConfig.FRIENDLYCODEEND.getValue()));
			result.add(new EnterpriseCustomField(eseId, PrintFileFieldConfig.SEQNUM.getName(),
					PrintFileFieldConfig.SEQNUM.getValue()));
			result.add(new EnterpriseCustomField(eseId, PrintFileFieldConfig.SEQNUMFRONT.getName(),
					PrintFileFieldConfig.SEQNUMFRONT.getValue()));
			result.add(new EnterpriseCustomField(eseId, PrintFileFieldConfig.SEQNUMEND.getName(),
					PrintFileFieldConfig.SEQNUMEND.getValue()));
			result.addAll(enterpriseCustomField);
			resultList.add(result);
			return resultList;

		default:
			return resultList;
		}
	}
 
	@Override
	public EnterpriseCustomFieldVO findFieldsDetailInfo(String fieldKey) {
		EnterpriseCustomFieldVO enterpriseCustomFieldVO = new EnterpriseCustomFieldVO();
		List<EnterpriseCustomField> result = enterpriseCustomFieldDao.findByfieldKey(fieldKey);
		StringBuilder eseIds = new StringBuilder();
		for (EnterpriseCustomField enterpriseCustomField : result) {
			eseIds.append(enterpriseCustomField.getEseId() + ",");
		}
		enterpriseCustomFieldVO.setFieldName(result.stream().findFirst().get().getFieldName());
		enterpriseCustomFieldVO.setFieldValue(result.stream().findFirst().get().getFieldValue());
		enterpriseCustomFieldVO.setEseIds(eseIds.substring(0, eseIds.length() - 1));
		enterpriseCustomFieldVO.setMode(MODE_SELECTION.CLASSIS_MODE.getValue());
		if (!fieldKey.startsWith("Classic_")) {
			enterpriseCustomFieldVO.setMode(MODE_SELECTION.COMMON_MODE.getValue());
		}
		return enterpriseCustomFieldVO;
	}

	/*
	 * @Transactional
	 * 
	 * @Override public void updateCustomField(EnterpriseCustomFieldVO
	 * enterpriseCustomFieldVO) { List<EnterpriseCustomField>
	 * result=enterpriseCustomFieldDao.findByfieldKey(enterpriseCustomFieldVO.
	 * getFieldKey()); for (EnterpriseCustomField customField : result) {
	 * enterpriseCustomFieldDao.delete(customField.getId()); } String[] eseIds=
	 * enterpriseCustomFieldVO.getEseIds().split(","); for (String eseId : eseIds) {
	 * if(eseId!=null){ EnterpriseCustomField
	 * enterpriseCustomField=enterpriseCustomFieldDao.findByfieldKeyAndEseId(
	 * enterpriseCustomFieldVO.getFieldKey(),Long.parseLong(eseId));
	 * if(enterpriseCustomField==null){ enterpriseCustomField=new
	 * EnterpriseCustomField(); enterpriseCustomField.setDeleted(false);
	 * enterpriseCustomField.setFieldKey(enterpriseCustomFieldVO.getFieldKey());
	 * enterpriseCustomField.setFieldName(enterpriseCustomFieldVO.getFieldName());
	 * enterpriseCustomField.setFieldValue(enterpriseCustomFieldVO.getFieldValue());
	 * } enterpriseCustomFieldDao.save(enterpriseCustomField); } }
	 * 
	 * }
	 */
	@Override
	public String findFieldResultInfo(String fieldValue) {
		TagCode tagCode = new TagCode();
		tagCode.setCode("27NHUOS4KNQY0K9*RW-");
		tagCode.setDomain("HTTP://DD.SAO.SO/");
		tagCode.setSubDomain("Q/");
		tagCode.setSeqNum(10000000000003076l);
		tagCode.setMixed("S-J2WCLR9S-67LD7");
		return CustomDefinitionParseUtils.parseCustomField(fieldValue, tagCode);
	}
}

// 文本中的Java对象
class StringJavaObject extends SimpleJavaFileObject {
	// 源代码
	private String content = "";

	// 遵循Java规范的类名及文件
	public StringJavaObject(String _javaFileName, String _content) {
		super(_createStringJavaObjectUri(_javaFileName), Kind.SOURCE);
		content = _content;
	}

	// 产生一个URL资源路径
	private static URI _createStringJavaObjectUri(String name) {
		// 注意此处没有设置包名 so/
		return URI.create("String:///" + name + Kind.SOURCE.extension);
	}

	// 文本文件代码
	@Override
	public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
		return content;
	}
}
